home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-13 | 5.0 KB | 293 lines | [TEXT/R*ch] |
- /*
- NewtonCanvas.java - A canvas for running a gravity simulation.
-
- Copyright (C) 1996-1997 by Michael J. Webb
- */
-
- // Imports
-
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
-
- import java.io.DataInputStream;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.io.StreamTokenizer;
-
- import java.util.Date;
- import java.util.Vector;
-
- import IdleTask;
- import TickerThread;
-
- /** A canvas for running a gravity simulation.
- */
- public class NewtonCanvas extends java.awt.Canvas
- implements IdleTask
- {
-
- /* Public members. */
-
- public int myNumRocks;
- public Rock[] myRocks;
-
- /* Construction/Destruction Methods. */
-
- /** Default constructor - make 100 random rocks.
- */
- public NewtonCanvas()
- {
- this(100, new Dimension(400, 400));
- }
-
- /** Randomizing constructor. Make <theNumRocks> rocks.
- */
- public NewtonCanvas
- (
- int theNumRocks,
- Dimension theSize
- )
- {
- setSize(theSize);
-
- myNumRocks = theNumRocks;
-
- myRocks = new Rock[theNumRocks];
-
- for (int i = 0; i < theNumRocks; i++)
- {
- myRocks[i] = new Rock(i, this);
- }
-
- }
-
- /* Public Methods. */
-
- /** Reseed the canvas with a new population of random rocks.
- */
- public void reseed()
- {
- for (int i = 0; i < myNumRocks; i++)
- {
- Rock.seed(myRocks[i]);
- }
-
- redraw(getGraphics());
- }
-
- /** Set the naughty flag of the canvas. Abuses garbage collection.
- */
- public void setNaughty(boolean naughty)
- {
- fNaughty = naughty;
- }
-
- /** Start the gravity simulation.
- */
- public synchronized void startTask()
- {
- fTime = new Date();
-
- if (fTicker == null)
- {
- fTicker = new TickerThread(this);
- fTicker.start();
- }
- }
-
- /** Stop the gravity simulation.
- */
- public synchronized void stopTask()
- {
- if (fTicker != null)
- {
- fTicker.stop();
- fTicker = null;
- }
- }
-
- /** Every time the simulation gets time, compute a new frame.
- */
- public synchronized void idle()
- {
- if (fRunning)
- {
- update(getGraphics());
- }
- }
-
- /** Pause the simulation.
- */
- public void pause()
- {
- fRunning = false;
- }
-
- /** Resume the simulation.
- */
- public void resume()
- {
- fTime = new Date();
- fFrames = 0;
-
- fRunning = true;
- }
-
- /** Compute the next frame of the simulation.
- */
- public void update(Graphics g)
- {
- // Compute new locations.
- for (int i = 0; i < myNumRocks; i++)
- {
- myRocks[i].computeValues();
- }
-
- // Redraw the canvas.
-
- redraw(g);
-
- // Update frame count.
- fFrames += 1;
- }
-
- /** Redraw the canvas.
- */
- public void redraw(Graphics g)
- {
- java.awt.Dimension d = getSize();
-
- // Check for resize.
- if (fOffScreenImage != null)
- {
- if
- (
- (fOffScreenImage.getHeight(this) != d.height) ||
- (fOffScreenImage.getWidth(this) != d.width)
- )
- {
- fOffScreenImage = null;
- }
- }
-
- // Build offscreen image, if necessary.
- if ((fOffScreenImage == null) || fNaughty)
- {
- fOffScreenImage = createImage(d.width,d.height);
- }
-
- // Setup the offscreen graphics.
- fOffScreenGraphics = fOffScreenImage.getGraphics();
- fOffScreenGraphics.setColor(getBackground());
- fOffScreenGraphics.fillRect(0,0,d.width,d.height);
-
- // Draw the rocks.
- for (int i = 0; i < myNumRocks; i++)
- {
- myRocks[i].drawSelfShape(fOffScreenGraphics);
- }
-
- // Move offscreen to screen.
- g.drawImage(fOffScreenImage, 0, 0, null);
- }
-
- /** Give the simulation's minimum size.
- */
- public java.awt.Dimension getMinimumSize()
- {
- return (new java.awt.Dimension(100, 100));
- }
-
- /** Give the simulation's preferred size.
- */
- public java.awt.Dimension getPreferredSize()
- {
- return (new java.awt.Dimension(400, 400));
- }
-
- /** Frame count accessor.
- */
- public long getFrames() { return fFrames; }
-
- /** Frame rate accessor.
- */
- public double getFrameRate()
- {
- double rate = 0.0;
- Date time = new Date();
- long elapsed = 0;
-
- if (fTime != null)
- {
- elapsed = time.getTime() - fTime.getTime();
- }
-
- if (elapsed != 0)
- {
- rate = ((double)fFrames) * 1000.0 / ((double)elapsed);
- }
-
- return rate;
- }
-
- /** Save a simulation.
- */
- public void store(java.io.PrintStream stream)
- {
- for (int i = 0; i < myNumRocks; i++)
- {
- myRocks[i].store(stream);
- }
- }
-
- /** Restore a simulation.
- */
- public void restore(java.io.DataInputStream stream)
- {
- Vector rockVector = new Vector(100, 10);
- Rock newRock = null;
- boolean reading = true;
- int rockCount = 0;
-
- InputStreamReader theReader = new InputStreamReader(stream);
- StreamTokenizer tokens = new StreamTokenizer(theReader);
-
- tokens.parseNumbers();
-
- while (reading)
- {
- try
- {
- newRock = new Rock(rockCount, this);
- newRock.restore(tokens);
- rockCount += 1;
- rockVector.addElement(newRock);
- }
- catch (Throwable e)
- {
- reading = false;
- }
- }
-
- myNumRocks = rockVector.size();
- myRocks = new Rock[myNumRocks];
- rockVector.copyInto(myRocks);
- }
-
- /* Private members. */
-
- private TickerThread fTicker = null;
- private boolean fYield = false;
-
- private Image fOffScreenImage = null;
- private Graphics fOffScreenGraphics = null;
-
- private long fFrames = 0;
- private Date fTime = null;
-
- private boolean fRunning = true;
- private boolean fNaughty = false;
- }
-